package WordCount;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

//流处理
public class WordCountStream {
    public static void main(String[] args) throws Exception {
        //创建流处理的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);//设置线程数

        //使用parameter tool工具从程序启动参数中提取配置
        ParameterTool parameterTool = ParameterTool.fromArgs(args);
        String host = parameterTool.get("host");
        int port = parameterTool.getInt("port");

        //从socket文本流读取数据
        DataStream<String> inputStream = env.socketTextStream(host, port);

        //基于数据流集训转换操作
        SingleOutputStreamOperator<Tuple2<String, Integer>> result = inputStream.flatMap(new WordCountTest01.MyFlatMapper())
                .keyBy(0)//与groupBy相似，按照key的hashCode
                .sum(1).setParallelism(3).slotSharingGroup("red");

        result.print().setParallelism(1);

        //启动任务，否则不会执行
        env.execute();
    }
}
